package com.vhr.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vhr.VO.OvertimeAdminVo;
import com.vhr.entities.Employee;
import com.vhr.entities.Overtime;
import com.vhr.entities.Result;
import com.vhr.service.IEmployeeService;
import com.vhr.service.IOvertimeService;
import com.vhr.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Name OvertimeController
 * @Description:
 * @Author: CHAO
 * @Date: 2019/9/27
 */
@RestController
@Api(description = "加班模块")
@CrossOrigin(allowCredentials="true",allowedHeaders="*")
public class OvertimeController {

    @Autowired
    private IOvertimeService iOvertimeService;
    @Autowired
    private IEmployeeService iEmployeeService;

    //查询所有加班记录
    @ApiOperation("管理员：查看所有员工的加班记录")
    @GetMapping("/api/overtime/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name="pageNum",required = false,value = "第几页",defaultValue = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name="pageSize",required = false,value = "一页有几条数据",defaultValue = "10",dataType = "Integer")
    })
    public Result overtimeList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<OvertimeAdminVo> list = iOvertimeService.selectByDeptAndStatusAndName(null,null,null);
        PageInfo<OvertimeAdminVo> pageInfo = new PageInfo<>(list);
        if (pageInfo.getList()==null){
            return Result.fail();
        }
        return Result.success().add("list",PageUtil.MyPageInfoUtil(pageInfo));
    }

    @ApiOperation("管理员：查看已操作的加班申请,1为已同意，2为已拒绝")
    @GetMapping("/api/overtime/operated")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name="pageNum",required = false,value = "第几页",defaultValue = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name="pageSize",required = false,value = "一页有几条数据",defaultValue = "10",dataType = "Integer")
    })
    public Result overtimeOperated(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                    @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        PageHelper.startPage(1,10);
        List<OvertimeAdminVo> list = iOvertimeService.selectOperated();
        PageInfo<OvertimeAdminVo> pageInfo = new PageInfo<>(list);
        return Result.success().add("list",PageUtil.MyPageInfoUtil(pageInfo));
    }

    @ApiOperation("管理员：查看未操作的加班申请,0为未审批")
    @GetMapping("/api/overtime/noOperated")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name="pageNum",required = false,value = "第几页",defaultValue = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name="pageSize",required = false,value = "一页有几条数据",defaultValue = "10",dataType = "Integer")
    })
    public Result overtimeNoOperated(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                   @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        PageHelper.startPage(1,10);
        List<OvertimeAdminVo> list = iOvertimeService.selectNoOperated();
        PageInfo<OvertimeAdminVo> pageInfo = new PageInfo<>(list);
        return Result.success().add("list",PageUtil.MyPageInfoUtil(pageInfo));
    }



    //根据id查询加班记录
    @ApiOperation("根据员工id查询加班信息")
    @GetMapping("/overtime/overtimeInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name="pageNum",required = false,value = "第几页",defaultValue = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name="pageSize",required = false,value = "一页有几条数据",defaultValue = "10",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query",name="id",required = true,value = "员工id",dataType = "Integer")
    })
    public Result overtimeInfo(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                @RequestParam(value = "id",required = true)Integer id){
        PageHelper.startPage(pageNum,pageSize);
        List<OvertimeAdminVo> list = iOvertimeService.selectPersonOvertime(id);
        PageInfo<OvertimeAdminVo> pageInfo = new PageInfo<>(list);
        for (OvertimeAdminVo o : pageInfo.getList()) {
            if(o.getOvertimeStatus()==0){
                o.setJudgeName(null);
            }
        }
        return Result.success().add("overtimeList", PageUtil.MyPageInfoUtil(pageInfo));
    }

    @ApiOperation("用户：插入加班信息")
    @PostMapping("/overtime/overtimeInsert")
    public Result overtimeInsert(@RequestBody Overtime overtime){
        int result = iOvertimeService.addOvertime(overtime);
        if(result==0){
            return Result.fail();
        }
        return Result.success();
    }
    //与前端整合需要改回@RequestBody
    @ApiOperation("管理员：同意加班申请，需要传入加班申请: overtimeId 和审批人ID: judgeId")
    @PutMapping("/api/overtime/agree")
    public Result agree(@RequestParam(value = "overtimeId",required = true) Integer overtimeId,
                        @RequestParam(value = "judgeId",required  = true) Integer judgeId){
        Overtime overtime = new Overtime();
        overtime.setOvertimeId(overtimeId);
        Employee admin = new Employee();
        admin.setEmpId(judgeId);
        int result = iOvertimeService.judegOvertime(overtime, admin, 1);
        if(result==1){
            return Result.success();
        }
        return Result.fail();
    }

    //与前端整合需要改回@RequestBody
    @ApiOperation("管理员：拒绝加班申请，需要传入加班申请: overtimeId 和审批人ID: judgeId")
    @PutMapping("/api/overtime/disagree")
    public Result disagree(@RequestParam(value = "overtimeId",required = true) Integer overtimeId,
                           @RequestParam(value = "judgeId",required  = true) Integer judgeId){
        Overtime overtime = new Overtime();
        overtime.setOvertimeId(overtimeId);
        Employee admin = new Employee();
        admin.setEmpId(judgeId);
        int result = iOvertimeService.judegOvertime(overtime, admin, 2);
        if(result==1){
            return Result.success();
        }
        return Result.fail();
    }

    @ApiOperation("管理员：删除加班申请")
    @DeleteMapping("/api/overtime/delete")
    public Result delete(Integer id){
        Overtime overtime = new Overtime();
        overtime.setOvertimeId(id);
        int result = iOvertimeService.deleteByPrimary(overtime);
        if(result==1){
            return Result.success();
        }
        return Result.fail();
    }

    @ApiOperation("管理员：根据部门id，加班申请状态和名字查询加班信息")
    @GetMapping("/api/overtime/selectByDeptAndStatusAndName")
    public Result  selectByDeptAndStatusAndName(Integer deptId,Integer status,String empName,
                                                @RequestParam(value = "pageNum",defaultValue = "1")  Integer pageNum,
                                                @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<OvertimeAdminVo> list = iOvertimeService.selectByDeptAndStatusAndName(deptId, status, empName);
        PageInfo<OvertimeAdminVo> pageInfo = new PageInfo<>(list);
        return  Result.success().add("overtimeList",PageUtil.MyPageInfoUtil(pageInfo));
    }
}
